<?php
if(!defined('IN_SITE')) exit('Access Denied');

class Page
{
	var $table;

	var $key;

	var $where;

	var $field;

	var $condition;

	var $page;

	var $pagesize;

	var $pagecount;

	var $recordcount;

	var $pgpdo;
	
	var $lang;
	
	var $page_record;

	function SetLang() {
		
		$lang['page_TableError'] = "数据表名不能为空";
		$lang['page_KeyError'] = "数据表主键名不能为空";
		$lang['page_error'] = "函数条件错误,数据表名,主键名不能为空,当前页为空或非法,字段需为一个数组或空";
		$lang['page_sum'] = "共";
		$lang['page_record'] = $this->page_record;
		$lang['page_pages'] = "页";
		$lang['page_fristpage'] = "首页";
		$lang['page_lastpage'] = "末页";
		$lang['page_precpage'] = "上一页";
		$lang['page_nextpage'] = "下一页";
		$lang['page_jumptopage'] = "第";
		
		$this->lang = $lang;
	}
	
	
     function Page($v=array()) {
     	
		$this->table = empty($v['table'])?$this->msg($this->lang['page_TableError']):$v['table'];

		$this->key = empty($v['key'])?$this->msg($this->lang['page_KeyError']):$v['key'];

		$this->where = empty($v['where'])?1:$v['where'];

		$this->field = empty($v['field'])?"*":implode(",",$v['field']);

		$this->condition = $v['condition']==""?" ORDER BY ".$v['key']." DESC":$v['condition'];//数据排列条件;

		$this->page = intval(empty($v['page'])?1:$v['page']);

		$this->pagesize = $v['pagesize']==""?10:$v['pagesize'];
		
		$this->params = $v['params'];
                
        $this->pgpdo = new PDO($GLOBALS['_CONFIG']['db']['dsn']);
		
		$this->page_record = empty($v['page_record'])?"条记录":$v['page_record'];
		
		$this->SetLang();
	 }

	function pages()
	{
		$SQL = "SELECT COUNT(".$this->key.") AS total FROM ".$this->table." WHERE ".$this->where."";

	    $result = $this->pgpdo->prepare($SQL);

		$result->execute();
		
		$rs = $result->fetch(PDO::FETCH_ASSOC);

	    $recordcount = $rs['total'];//总记录集

		$pagecount = ceil($recordcount/$this->pagesize);//总页数

		$this->page = $this->page>$pagecount?$pagecount:$this->page;

		$this->page= $this->page<1?1:$this->page;

		$start = ($this->page-1)*$this->pagesize;

        $this->recordcount = $recordcount;

		$this->pagecount = $pagecount;

		$pages = array();

       	$SQL = "SELECT ".$this->field." FROM ".$this->table." WHERE ".$this->where.$this->condition." LIMIT ".$start.",".$this->pagesize."";
		
       	$result = $this->pgpdo->prepare($SQL);

		$result->execute();
		
		$pages = $result->fetchAll(PDO::FETCH_ASSOC);
		
		return $pages;
	}

	function SetPageStr() {
		if(!$this->pagecount) {
			$SQL = "SELECT COUNT(".$this->key.") AS total FROM ".$this->table." WHERE ".$this->where."";
	
		    $result = $this->pgpdo->prepare($SQL);
	
			$result->execute();
			
			$rs = $result->fetch(PDO::FETCH_ASSOC);
	
		    $recordcount = $rs['total'];//总记录集
	
			$pagecount = ceil($recordcount/$this->pagesize);//总页数
		
	        $this->recordcount = $recordcount;
	
			$this->pagecount = $pagecount ? $pagecount : 1;
		}
		$PageStr = "";
		//url处理
		//$pageurl = $_SERVER['PHP_SELF'];
		//if(isset($_SERVER['QUERY_STRING']))
		//{
		//	$pageurl .= '?' . preg_replace('/&page=[0-9]*/','',$_SERVER['QUERY_STRING']) . '&';
		//}
		$pageurl = $this->params ? '?' . $this->params . '&' : '?';
		
		$PageStr .= $this->lang['page_sum']."<strong>".$this->recordcount."</strong>".$this->lang['page_record']."&nbsp;&nbsp;".$this->lang['page_sum']."<strong>".$this->pagecount."</strong>".$this->lang['page_pages']."&nbsp;&nbsp;".$this->page."/".$this->pagecount."&nbsp;&nbsp;";

		$PageStr .= $this->page !=1?"<a href='".$pageurl."page=1'>".$this->lang['page_fristpage']."</a>":$this->lang['page_fristpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page>1?"<a href='".$pageurl."page=".($this->page-1)."'>".$this->lang['page_precpage']."</a>":$this->lang['page_precpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page<$this->pagecount?"<a href='".$pageurl."page=".($this->page+1)."'>".$this->lang['page_nextpage']."</a>":$this->lang['page_nextpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page<$this->pagecount?"<a href='".$pageurl."page=".$this->pagecount."'>".$this->lang['page_lastpage']."</a>":$this->lang['page_lastpage'];
		/*
		$PageStr .= "&nbsp;&nbsp;".$this->lang['page_jumptopage'];

		$PageStr .= "<select name=\"PageSelect\" onchange=\"javascript:location.href='".$pageurl."page='+this.options[this.options.selectedIndex].value+'"."'\" >";

		for($i=1;$i<=$this->pagecount;$i++)
		{
	      if($i==$this->page)
		  {
	          $selected = "selected";
	      }else
		  {
	          $selected = "";
	      }
	      $PageStr.= "<option value='".$i."' ".$selected.">".$i."</option>";
	    }

	    $PageStr.="</select>".$this->lang['page_pages'];
		*/
		global $_THEME;
		if($this->pagecount > 0) {
			$PageStr .= '&nbsp;&nbsp;<div class="PageSelect" onmouseover="javascript:document.getElementById(\'PageSelectList\').style.display=\'block\'" onmouseout="javascript:document.getElementById(\'PageSelectList\').style.display=\'none\'" style="width:80px;float:right;cursor:pointer;position:relative;margin-right:20px;*top:-15px;text-align:center">'.$this->lang['page_jumptopage'].$this->page.$this->lang['page_pages'].'&nbsp;<img src="'.$_THEME.'/images/pageicon.gif" />';
		    $PageStr .= '<ul class="PageSelectList" id="PageSelectList" style="background:#fff;display:none;left:0px;position:absolute;border:1px solid #aaa;*margin-top:15px;left:8px;">';
			for($i=1;$i<=$this->pagecount;$i++) {
		      if($i==$this->page) {
		          $selected = "selected";
		      }
		      else {
		          $selected = "";
		      }
		      $PageStr.= '<li class="'.$selected.'" style="padding:2px 15px;"><a href="'.$pageurl.'page='.$i.'" >'.$this->lang['page_jumptopage'].$i.$this->lang['page_pages'].'</a></li>';
		    }
		    $PageStr .= '</ul></div>';
		}
		return $PageStr;

	}

	function SetPageStrDd()
	{

		$PageStr = "";
		//url处理
		$pageurl = $_SERVER['PHP_SELF'];
		if(isset($_SERVER['QUERY_STRING']))
		{
			$pageurl .= '?' . preg_replace('/&page=[0-9]*/','',$_SERVER['QUERY_STRING']) . '&';
		}

		$PageStr .= $this->lang['page_sum']."<strong>".$this->recordcount."</strong>".$this->lang['page_record']."&nbsp;&nbsp;".$this->lang['page_sum']."<strong>".$this->pagecount."</strong>".$this->lang['page_pages']."&nbsp;&nbsp;".$this->page."/".$this->pagecount."&nbsp;&nbsp;";

		$PageStr .= $this->page !=1?"<a href='".$pageurl."page=1'>".$this->lang['page_fristpage']."</a>":$this->lang['page_fristpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page>1?"<a href='".$pageurl."page=".($this->page-1)."'>".$this->lang['page_precpage']."</a>":$this->lang['page_precpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page<$this->pagecount?"<a href='".$pageurl."page=".($this->page+1)."'>".$this->lang['page_nextpage']."</a>":$this->lang['page_nextpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page!=$this->pagecount?"<a href='".$pageurl."page=".$this->pagecount."'>".$this->lang['page_lastpage']."</a>":$this->lang['page_lastpage'];
		
		$PageStr .= "&nbsp;&nbsp;".$this->lang['page_jumptopage'];

		$PageStr .= "<select name=\"PageSelect\" onchange=\"javascript:location.href='".$pageurl."page='+this.options[this.options.selectedIndex].value+'"."'\" >";

		for($i=1;$i<=$this->pagecount;$i++)
		{
	      if($i==$this->page)
		  {
	          $selected = "selected";
	      }else
		  {
	          $selected = "";
	      }
	      $PageStr.= "<option value='".$i."' ".$selected.">".$i."</option>";
	    }

	    $PageStr.="</select>".$this->lang['page_pages'];
	    
		return $PageStr;

	}
    function SetPageStrHC()
	{

		$PageStr = "";
		//url处理
		$pageurl = $_SERVER['PHP_SELF'];
		if(isset($_SERVER['QUERY_STRING']))
		{
			$pageurl .= '?' . preg_replace('/&page=[0-9]*/','',$_SERVER['QUERY_STRING']) . '&';
		}

		$PageStr .= '<div class="FenYe">'.$this->lang['page_sum']."<strong>".$this->recordcount."</strong>".$this->lang['page_record']."&nbsp;&nbsp;".$this->lang['page_sum']."<strong>".$this->pagecount."</strong>".$this->lang['page_pages']."&nbsp;&nbsp;".$this->page."/".$this->pagecount."&nbsp;&nbsp;";

		$PageStr .= $this->page !=1?"<a href='".$pageurl."page=1'>".$this->lang['page_fristpage']."</a>":$this->lang['page_lastpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page>1?"<a href='".$pageurl."page=".($this->page-1)."'>".$this->lang['page_precpage']."</a>":$this->lang['page_precpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page<$this->pagecount?"<a href='".$pageurl."page=".($this->page+1)."'>".$this->lang['page_nextpage']."</a>":$this->lang['page_nextpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page!=$this->pagecount?"<a href='".$pageurl."page=".$this->pagecount."'>".$this->lang['page_lastpage']."</a>":$this->lang['page_lastpage'];
		
		$PageStr .= "&nbsp;&nbsp;".$this->lang['page_jumptopage'];
		
		$PageStr .= "<select name=\"PageSelect\" onchange=\"javascript:location.href='".$pageurl."page='+this.options[this.options.selectedIndex].value+'"."'\" >";

		for($i=1;$i<=$this->pagecount;$i++)
		{
	      if($i==$this->page)
		  {
	          $selected = "selected";
	      }else
		  {
	          $selected = "";
	      }
	      $PageStr.= "<option value='".$i."' ".$selected.">".$i."</option>";
	    }

	    $PageStr.="</select>".$this->lang['page_pages'];
	    
	    $PageStr.='</div>';

		return $PageStr;

	}
		function _SetPageStr($page = 3, $simple = 0, $onclick = '')
		{
			$multipage = '';
			$onclick = $onclick ? ' onclick="'.$onclick.'(event)"' : '';
			//url处理
			$mpurl = $_SERVER['PHP_SELF'];
			if(isset($_SERVER['QUERY_STRING']))
			{
				$mpurl .= '?' . preg_replace('/&page=[0-9]*/','',$_SERVER['QUERY_STRING']) . '&';
			}
			if($this->recordcount > $this->pagesize) {
				$offset = 2;



				if($page > $this->pagecount) {
					$from = 1;
					$to = $this->pagecount;
				} else {
					$from = $this->page - $offset;



					$to = $from + $page - 1;
					if($from < 1) {
						$to = $this->page + 1 - $from;
						$from = 1;
						if($to - $from < $page) {
							$to = $page;
						}
					} elseif($to > $this->pagecount) {
						$from = $this->pagecount - $page + 1;
						$to = $this->pagecount;
					}
				}

				$multipage = ($this->page - $offset > 1 && $this->pagecount > $page ? '<a href="'.$mpurl.'page=1" '.$onclick.'>&lsaquo;</a>' : '').
					($this->page > 1 && !$simple ? '<a href="'.$mpurl.'page='.($this->page - 1).'" class="next">&lt;&lt; Previous</a>' : '');
				for($i = $from; $i <= $to; $i++) {
					$multipage .= $i == $this->page ? '<SPAN class=current>'.$i.'</span>' :
						'<a href="'.$mpurl.'page='.$i.'" '.$onclick.'>'.$i.'</a>';
				}

				$multipage .= ($this->page < $this->pagecount && !$simple ? '<a href="'.$mpurl.'page='.($this->page + 1).'" class="next"'.$onclick.'>Next&lt;&lt;</a>' : '').
					($to < $this->pagecount ? '<a href="'.$mpurl.'page='.$this->pagecount.'" '.$onclick.'>&rsaquo;</a>' : '');

				$multipage = $multipage ? '<span>'.(!$simple ? '<DIV class=pages-nav style="PADDING-RIGHT: 0px! important; PADDING-LEFT: 0px !important; PADDING-BOTTOM: 10px! important; MARGIN: 10px 7px 0px 20px; PADDING-TOP: 0px! important"><a >&nbsp;'.$this->lang['page_sum'].''.$this->recordcount.''.$this->lang['page_record'].'&nbsp;</a><a >&nbsp;'.$this->page.'/'.$this->pagecount.''.$this->lang['page_pages'].'&nbsp;</a>' : '').$multipage.'</span>' : '';
			}
			return $multipage;
		}


		function AjaxSetPageStr($page = 5, $simple = 0, $onclick = '')
		{
			$multipage = '';

			//url处理
			$mpurl = $_SERVER['PHP_SELF'];
			if(isset($_SERVER['QUERY_STRING']))
			{
				$mpurl .= '?' . preg_replace('/&page=[0-9]*/','',$_SERVER['QUERY_STRING']) . '&';
			}
			if($this->recordcount > $this->pagesize) {
				$offset = 2;



				if($page > $this->pagecount) {
					$from = 1;
					$to = $this->pagecount;
				} else {
					$from = $this->page - $offset;



					$to = $from + $page - 1;
					if($from < 1) {
						$to = $this->page + 1 - $from;
						$from = 1;
						if($to - $from < $page) {
							$to = $page;
						}
					} elseif($to > $this->pagecount) {
						$from = $this->pagecount - $page + 1;
						$to = $this->pagecount;
					}
				}

				$multipage = ($this->page - $offset > 1 && $this->pagecount > $page ? '<a href="javascript:AjaxPage('.(1).')">&lsaquo;</a>' : '').
					($this->page > 1 && !$simple ? '<a href="javascript:AjaxPage('.($this->page-1).')" class="next">&lt;&lt; Previous</a>' : '');
				for($i = $from; $i <= $to; $i++) {
					$multipage .= $i == $this->page ? '<SPAN class=current>'.$i.'</span>' :
						'<a href="javascript:AjaxPage('.$i.')">'.$i.'</a>';
				}

				$multipage .= ($this->page < $this->pagecount && !$simple ? '<a href="javascript:AjaxPage('.($this->page+1).')" class="next">Next&lt;&lt;</a>' : '').
					($to < $this->pagecount ? '<a href="javascript:AjaxPage('.($this->pagecount).')">&rsaquo;</a>' : '');

				$multipage = $multipage ? '<span>'.(!$simple ? '<DIV class=pages-nav style="PADDING-RIGHT: 0px! important; PADDING-LEFT: 0px !important; PADDING-BOTTOM: 10px! important; MARGIN: 10px 7px 0px 20px; PADDING-TOP: 0px! important"><a >&nbsp;'.$this->lang['page_sum'].''.$this->recordcount.''.$this->lang['page_record'].'&nbsp;</a><a >&nbsp;'.$this->page.'/'.$this->pagecount.''.$this->lang['page_pages'].'&nbsp;</a>' : '').$multipage.'</span>' : '';
				$multipage.='</div></div>';
			}
			return $multipage;
		}


}



?>